//
//  QuestionGroupBuilder.swift
//  RabbleWabble
//
//  Created by TRS-Mobile on 2021/9/9.
//

import Foundation

class QuestionBuilder {
    var answer = ""
    var hint = ""
    var prompt = ""
    
    func build() throws -> Question {
        guard answer.count > 0 else {
            throw Error.missingAnswer
        }
        guard prompt.count > 0 else {
            throw Error.missingPrompt
        }
        return Question(answer: answer, hint: hint, prompt: prompt)
    }
    
    enum Error: String, Swift.Error {
        case missingAnswer
        case missingPrompt
    }
}

class QuestionGroupBuilder {
    var questions: [QuestionBuilder] = []
    var title = ""
    func addNew() {
        let question = QuestionBuilder()
        return  questions.append(question)
    }
    
    func removeQuestion(at index: Int) {
        questions.remove(at: index)
    }
    
    func build() throws -> QuestionGroup {
        guard title.count > 0 else {
            throw Error.missingTitle
        }
        guard questions.count > 0 else {
            throw Error.missingQuestions
        }
        let questions = try questions.map { try $0.build() }
        return QuestionGroup(questions: questions, title: title)
    }
    
    enum Error: String, Swift.Error {
        case missingTitle
        case missingQuestions
    }
}

